package com.dycong.common.network;

import lombok.extern.log4j.Log4j;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.log4j.Logger;

import javax.net.ssl.SSLContext;
import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

/**
 * Created by Yu on 2014/11/25.
 */
@Log4j
public class HttpStatusClient extends AbstractHttpClient {

    public static SSLContext sslContext;

    static Logger logger = Logger.getLogger(HttpStatusClient.class);

    protected Map<String, String> paramMap = new HashMap<>();
    protected Map<String, String> headerMap = new HashMap<>();
    protected Map<String, String> cookieMap = new HashMap<>();

    Optional<String> referer = Optional.empty();

    HttpClient client=sslContext==null?(HttpClients.createDefault()):(HttpClients.custom().setSSLContext(sslContext).build()) ;

    static {
        try {
            SSLContextBuilder contextBuilder=SSLContextBuilder.create();
            File certFiles= Paths.get(HttpStatusClient.class.getResource("/cacerts").toURI()).toFile();
            contextBuilder.loadTrustMaterial(certFiles,null,new TrustSelfSignedStrategy());
            sslContext=contextBuilder.build();
        }catch (Exception e){
            log.error("error load ssl cacerts "+e);
        }
    }

    public void setParamMap(Map<String, String> paramMap) {
        this.paramMap = paramMap;
        if (this.paramMap == null) {
            this.paramMap = new HashMap<>();
        }
    }

    public void addParam(String key, String value) {
        if (value == null) {
            value = "";
        }
        this.paramMap.put(key, value);
    }

    public void setHeaderMap(Map<String, String> headerMap) {
        this.headerMap = headerMap;
        if (this.headerMap == null) {
            this.headerMap = new HashMap<>();
        }
    }

    public void addHeader(String key, String value) {
        headerMap.put(key, value);
    }

    @Override
    public String doGet(String url, String encoding) throws IOException {
        HttpGet httpGet = new HttpGet(url);
        genRequestEntity(httpGet, encoding, headerMap, paramMap);
        return execute(client, httpGet, encoding);
    }


    @Override
    public String doPost(String url, String encoding) throws IOException {
        HttpPost post = new HttpPost(url);
        genRequestEntity(post, encoding, headerMap, paramMap);
        return execute(client, post, encoding);
    }

    @Override
    public String doPost(String url, String encoding, Map<String, String> headerMap, Map<String, String> parameters) throws IOException {
        setParamMap(parameters);
        setHeaderMap(headerMap);
        return doPost(url, encoding);
    }

    @Override
    public String doGet(String url, String encoding, Map<String, String> headerMap) throws IOException {
        setHeaderMap(headerMap);
        return doGet(url, encoding);
    }


    public static void main(String... args) throws IOException {
        HttpStatusClient client1 = new HttpStatusClient();
        String response = client1.doGet("http://eheobo.com");
        System.out.println(response);
    }


    public void resetParams() {
        this.paramMap = new HashMap<>();
    }

    @Override
    public String postJson(String url, String encoding, String json) throws IOException {
        return postJson(url, encoding, headerMap, json);
    }

    @Override
    protected HttpClient getHttpClient() {
        return client;
    }

    public String postJson(String url, String json) {
        try {
            return postJson(url, "utf-8", json);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


}
